//
//  HEEduVideoRecommView.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/18.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEEduVideoRecommView.h"
#import "WaterFlowLayout.h"
#import "HEFlowVideoCell.h"

@interface HEEduVideoRecommView ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, WaterFlowLayoutDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) WaterFlowLayout *layout;
//@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@property (nonatomic, assign) BOOL hasLoad;

@end

static NSString * const HEGoodsRecomCellId = @"HEGoodsRecomCell";


@implementation HEEduVideoRecommView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self=[super initWithFrame:frame]) {
        self.clipsToBounds = YES;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    // 1.创建流水布局
//    _layout = [[WSLWaterFlowLayout alloc] init];
//    _layout.delegate = self;
//    _layout.flowLayoutStyle = WSLWaterFlowVerticalEqualWidth;
  
    _layout = [[UICollectionViewFlowLayout alloc] init];

    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    if (@available(iOS 11.0, *)) {
        _collectionView.contentInsetAdjustmentBehavior = NO;
    } else {
        // Fallback on earlier versions
    }
    [self addSubview:_collectionView];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self);
    }];
    
    /// 设置此属性为yes 不满一屏幕 也能滚动
    self.collectionView.alwaysBounceHorizontal = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    [_collectionView registerClass:HEFlowVideoCell.class forCellWithReuseIdentifier:KHEFlowVideoCell];
}

- (void)setGoodsArr:(NSMutableArray<HEGoodsModel *> *)goodsArr {
    
    if (_hasLoad) {
        return;
    }
    
    _hasLoad = YES;

    _goodsArr = goodsArr;
    [self.collectionView reloadData];
    

    // 返回collectionView真实高度
    if (self.heightBlock) {
        self.heightBlock(self.layout.collectionViewContentSize.height);
    }
}


- (CGFloat)getHeight {
    [_collectionView reloadData];
    return self.layout.collectionViewContentSize.height;
}

- (void)getFooterViewHeightBlock:(getViewHeightBlock)block {
    _heightBlock = block;
}

//解决ios8上自动布局的问题

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return YES;
}


#pragma mark - 代理方法 Delegate Methods
// 设置分区

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
///** 列数*/
//- (CGFloat)columnCountInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout

// 每个分区上得元素个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.goodsArr.count;
}

// 设置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HEFlowVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: KHEFlowVideoCell forIndexPath:indexPath];
//    cell.model = self.goodsArr[indexPath.row];
     return cell;
}

// 设置cell大小 itemSize：可以给每一个cell指定不同的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
 //    CGFloat width = [self gainStringWidthWithString:self.nameArray[indexPath.row] font:15.0f height:height];
    return CGSizeMake(120, 200);
}


// 设置UIcollectionView整体的内边距（这样item不贴边显示）
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // 上 左 下 右
    return UIEdgeInsetsMake(0, 15 , 0, 15);
}

// 设置minimumLineSpacing：cell上下之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

// 设置minimumInteritemSpacing：cell左右之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

// 选中cell的回调
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationToGoodsDetails object:self.goodsArr[indexPath.row]];
}

#pragma mark - WaterFlowout代理，请填入返回多少列
- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfLineForSection:(NSInteger)section {
    return 2;
}

@end
 
